<?php
!defined('IN_FRAME') && die('404 Page');
class c_content extends Controller {
    
    private $arrCurManager;
    private $mdlManager;
    private $mdlAcl;
    private $mdlCat;
    private $mdlContent;
    
    /**
     * 构造函数
     */
    public function __construct() {
        $this->mdlAcl = Helper::loadModel('acl');
        $this->mdlCat = Helper::loadModel('cat');
        $this->mdlManager = Helper::loadModel('manager');
        $this->mdlContent = Helper::loadModel('content');
        if (!$this->mdlManager->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } else {
            $this->arrCurManager = $this->mdlManager->getSession();
        }
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'view')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权查看此模块!');
        }
    }
    
    public function clean() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'clean')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=content', '你无权删除数据!');
        }
        $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
        $strMsg = '参数错误!';
        if ($id > 0) {
            $this->mdlContent->delete($id);
            $mdlPage = new m_content_page();
            $mdlPage->delete(array('content_id' => $id));
            $strMsg = '删除成功!';
        }
        Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=content', $strMsg);
    }
    
    public function clean_page() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content_page', 'clean')) {
            Helper::jsCloseWindow('你无权删除数据!');
        }
        $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
        $strMsg = '参数错误!';
        if ($id > 0) {
            $mdlPage = new m_content_page();
            $mdlPage->delete($id);
            $strMsg = '删除成功!';
        }
        Helper::jsCloseWindow($strMsg, 'window.opener.location.reload();');
    }
    
    public function top() {
        $arrRe = array(
            'ok' => 'no',
            'msg' => '参数错误!'
        );
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'sort')) {
            $arrRe['msg'] = '你无权置顶!';
        } elseif (isset($_POST['id']) && isset($_POST['val']) && in_array($_POST['val'], array(0, 1))) {
            $id = substr($_POST['id'], 7);
            if (is_numeric($id)) {
                $id = intval($id);
                $val = intval($_POST['val']);
                if ($id > 0 && $val >= 0) {
                    $this->mdlContent->update(array('is_top' => $val), $id);
                    $arrRe['ok'] = 'yes';
                    $arrRe['msg'] = '';
                }
            }
        }
        die(json_encode($arrRe));
    }
    
    public function move() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'update')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权移动!');
        }
        $objTreeList = $this->mdlCat->objTreeList;
        $arrAssign = array(
            'strTitle' => '批量移动',
            'strMsg' => '',
            'strOk' => '',
            'objTreeList' => $objTreeList
        );
        if (isset($_POST['sub_btn'])) {
            if (isset($_POST['from']) && is_array($_POST['from']) && count($_POST['from']) > 0) {
                if (isset($_POST['to']) && is_numeric($_POST['to'])) {
                    if ($this->mdlContent->update(array('cat_id' => $_POST['to']), '`cat_id` IN (' . addslashes(implode(',', $_POST['from'])) . ')')) {
                        $arrAssign['strOk'] = '移动完成!';
                    }
                }
            }
        }
        $this->_display('admin/content_move.php', $arrAssign);
    }
    
    public function sort_page() {
        $arrRe = array(
            'ok' => 'no',
            'msg' => '参数错误!'
        );
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content_page', 'sort')) {
            $arrRe['msg'] = '你无权排序!';
        } elseif (isset($_POST['id']) && isset($_POST['val']) && is_numeric($_POST['val'])) {
            $id = substr($_POST['id'], 3);
            if (is_numeric($id)) {
                $id = intval($id);
                $val = intval($_POST['val']);
                if ($id > 0 && $val >= 0) {
                    $mdlPage = new m_content_page();
                    $mdlPage->update(array('list_order' => $val), $id);
                    $arrRe['ok'] = 'yes';
                    $arrRe['msg'] = '';
                }
            }
        }
        die(json_encode($arrRe));
    }
    
    public function sort() {
        $arrRe = array(
            'ok' => 'no',
            'msg' => '参数错误!'
        );
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'sort')) {
            $arrRe['msg'] = '你无权排序!';
        } elseif (isset($_POST['id']) && isset($_POST['val']) && is_numeric($_POST['val'])) {
            $id = substr($_POST['id'], 3);
            if (is_numeric($id)) {
                $id = intval($id);
                $val = intval($_POST['val']);
                if ($id > 0 && $val >= 0) {
                    $this->mdlContent->update(array('list_order' => $val), $id);
                    $arrRe['ok'] = 'yes';
                    $arrRe['msg'] = '';
                }
            }
        }
        die(json_encode($arrRe));
    }
    
    public function index() {
        $objTreeList = $this->mdlCat->objTreeList;
        $arrAssign = array(
            'strTitle' => '内容列表',
            'mdlCat' => $this->mdlCat,
            'objTreeList' => $objTreeList,
            'strKeyword' => isset($_GET['keyword']) && !empty($_GET['keyword']) ? $_GET['keyword'] : '',
            'arrList' => array(),
            'catId' => isset($_GET['cat_id']) ? intval($_GET['cat_id']) : -1
        );
        $strWhere = '';
        if ($arrAssign['catId'] > 0) {
            $strWhere = '`cat_id` IN (';
            $arrTemp = array($arrAssign['catId']);
            if (isset($objTreeList->arrIdChildren['_' . $arrAssign['catId']])) {
                $arrTemp = array_merge($arrTemp, $objTreeList->arrIdChildren['_' . $arrAssign['catId']]);
            }
            $strWhere .= str_replace('_', '', implode(', ', $arrTemp)) . ')';
        } elseif ($arrAssign['catId'] == 0) {
            $strWhere = '`cat_id` = 0';
        } else {
            $strWhere = '`cat_id` >= 0';
        }
        if (!empty($arrAssign['strKeyword'])) {
            $strWhere .= ' AND MATCH (`search_index`) AGAINST ("' . Helper::genIndexStr($arrAssign['strKeyword']) . '")';
        }
        $intCurPage = (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) ? intval($_GET['page']) : 1;
        $objPager = new Pager(HTTP_URL . '/index.php', $_GET, $this->mdlContent->count($strWhere));
        $intCntPerPage = 10;
        $objPager->intItemsPerPage = $intCntPerPage;
        $objPager->strHtmlStart = '<li>';
        $objPager->strHtmlEnd = '</li>';
        $objPager->strCurrCssClass = 'active';
        $objPager->strDivCssClass = 'pagination pagination-centered';
        $objPager->strDivExt = 'style="margin-top:0px; margin-bottom:0px;"';
        $objPager->blnNeedMore = false;
        $arrAssign['objPager'] = $objPager;
        $arrAssign['arrList'] = $this->mdlContent->select($strWhere, '*', 'ORDER BY `id` DESC LIMIT ' . ($intCurPage - 1) * $intCntPerPage . ', ' . $intCntPerPage);
        $this->_display('admin/content_index.php', $arrAssign);
    }
    
    public function edit() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'update')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权编辑内容!');
        }
        $objTreeList = $this->mdlCat->objTreeList;
        $arrAssign = array(
            'blnAdd' => false,
            'strTitle' => '编辑内容',
            'objTreeList' => $objTreeList,
            'arrData' => array(),
            'id' => isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0
        );
        $arrData = array();
        if ($arrAssign['id'] > 0) {
            $arrData = $this->mdlContent->getOne($arrAssign['id']);
        }
        if (!$arrData) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=content', '参数错误!');
        } else {
            $arrAssign['arrData'] = $arrData;
            $mdlPage = Helper::loadModel('content_page');
            $arrAssign['arrPageList'] = $mdlPage->select(array('content_id' => $arrAssign['id']), '*', 'ORDER BY `list_order`');
        }
        if (isset($_POST['sub_btn'])) {
            $bln = true;
            $strMsg = '';
            $arrUpdate = array();
            if (!isset($_POST['cat_id']) || ($_POST['cat_id'] > 0 && !isset($objTreeList->arrIdAll['_' . $_POST['cat_id']]))) {
                $bln = false;
                $strMsg = '栏目参数错误!';
            }
            if ($bln) {
                if ($_POST['cat_id'] > 0) {
                    $arrTemp = $objTreeList->arrIdAll['_' . $_POST['cat_id']];
                    if ($arrTemp['rec_status'] == 0) {
                        $bln = false;
                        $strMsg = '栏目现不可用,请检查!';
                    } else {
                        $arrUpdate['cat_id'] = $_POST['cat_id'];
                    }
                } else {
                    $arrUpdate['cat_id'] = 0;
                }
            }
            if ($bln) {
                if (!isset($_POST['title']) || empty($_POST['title'])) {
                    $bln = false;
                    $strMsg = '请输入内容标题!';
                } else {
                    $arrUpdate['title'] = $_POST['title'];
                }
            }
            if ($bln) {
                isset($_POST['source_from']) && $arrUpdate['source_from'] = $_POST['source_from'];
                isset($_POST['thumb']) && $arrUpdate['thumb'] = $_POST['thumb'];
                isset($_POST['desc']) && $arrUpdate['desc'] = $_POST['desc'];
                isset($_POST['tags']) && $arrUpdate['tags'] = $_POST['tags'];
                isset($_POST['seo_t']) && $arrUpdate['seo_t'] = $_POST['seo_t'];
                empty($arrUpdate['seo_t']) && $arrUpdate['seo_t'] = $arrUpdate['title'];
                isset($_POST['seo_k']) && $arrUpdate['seo_k'] = $_POST['seo_k'];
                empty($arrUpdate['seo_k']) && $arrUpdate['seo_k'] = $arrUpdate['tags'];
                isset($_POST['seo_d']) && $arrUpdate['seo_d'] = $_POST['seo_d'];
                $strFenCi = Helper::fenCi($arrUpdate['title'] . ' ' . $arrUpdate['desc'] . ' ' . $arrUpdate['tags']);
                $arrUpdate['search_index'] = Helper::genIndexStr($strFenCi);
                $arrUpdate['last_updated'] = date('Y-m-d H:i:s');
                $arrUpdate['rec_status'] = isset($_POST['rec_status']) ? $_POST['rec_status'] : 0;
                if ($arrData['rec_status'] != $arrUpdate['rec_status']) {
                    if ($arrUpdate['rec_status'] == 1) {
                        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'back')) {
                            $bln = false;
                            $strMsg = '你无权限还原数据!';
                        }
                    } else {
                        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'delete')) {
                            $bln = false;
                            $strMsg = '你无权限设置数据可用状态!';
                        }
                    }
                }
                if ($bln) {
                    if ($this->mdlContent->update($arrUpdate, $arrData['id'])) {
                        $arrAssign['strOk'] = '保存成功!';
                        $arrAssign['arrData'] = $this->mdlContent->getOne($arrData['id']);
                    } else {
                        $strMsg = '未能保存,请检查!';
                    }
                }
            }
            $arrAssign['strMsg'] = $strMsg;
        }
        $this->_display('admin/content_edit.php', $arrAssign);
    }
    
    public function edit_page() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content_page', 'update')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权添加内容!');
        }
        $arrAssign = array(
            'arrData' => array(),
            'strMsg' => '',
            'strOk' => '',
            'blnAdd' => false,
            'strTitle' => '编辑内容页',
            'strOk' => '',
    		'content_id' => isset($_GET['content_id']) && is_numeric($_GET['content_id']) ? intval($_GET['content_id']) : 0,
    		'page_id' => isset($_GET['page_id']) && is_numeric($_GET['page_id']) ? intval($_GET['page_id']) : 0
        );
        $arrTemp = array();
        $mdlPage = new m_content_page();
        if ($arrAssign['content_id'] > 0 && $arrAssign['page_id'] > 0) {
            $arrTemp = $this->mdlContent->getOne($arrAssign['content_id']);
        }
        if (!$arrTemp) {
            Helper::jsCloseWindow('参数错误!');
        } else {
            $arrAssign['arrContentData'] = $arrTemp;
            $arrTemp = $mdlPage->getOne($arrAssign['page_id']);
        }
        if (!$arrTemp) {
            Helper::jsCloseWindow('参数错误!');
        } else {
            $arrAssign['arrData'] = $arrTemp;
        }
        if (isset($_POST['sub_btn'])) {
            $arrUpdate = array(
                'last_updated' => date('Y-m-d H:i:s'),
                'user_id' => $this->arrCurManager['id'],
                'user_name' => $this->arrCurManager['nick_name'],
                'title' => isset($_POST['title']) ? $_POST['title'] : '',
                'txt' => isset($_POST['txt']) ? $_POST['txt'] : '',
            );
            $strFenCi = Helper::fenCi($arrUpdate['title'] . ' ' . strip_tags($arrUpdate['txt']));
            $arrUpdate['search_index'] = Helper::genIndexStr($strFenCi);
            $arrUpdate['rec_status'] = isset($_POST['rec_status']) ? $_POST['rec_status'] : 0;
            if ($mdlPage->update($arrUpdate, $arrAssign['page_id'])) {
                Helper::jsCloseWindow('保存成功!', 'window.opener.location.reload();');
            } else {
                $arrAssign['arrData']['title'] = $arrUpdate['title']; 
                $arrAssign['arrData']['txt'] = $arrUpdate['txt'];
                $arrAssign['arrData']['rec_status'] = $arrUpdate['txt'];
                $arrAssign['strMsg'] = '保存失败,请检查!';
            }
        }
        $this->_display('admin/content_page.php', $arrAssign);
    }
    
    
    public function add_page() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content_page', 'add')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权添加内容!');
        }
        $arrAssign = array(
            'arrData' => array(
                'title' => '',
                'txt' => ''
            ),
            'strMsg' => '',
            'strOk' => '',
            'blnAdd' => true,
            'strTitle' => '添加内容页',
            'strOk' => '',
    		'content_id' => isset($_GET['content_id']) && is_numeric($_GET['content_id']) ? intval($_GET['content_id']) : 0
        );
        $arrTemp = array();
        if ($arrAssign['content_id'] > 0) {
            $arrTemp = $this->mdlContent->getOne($arrAssign['content_id']);
        }
        if (!$arrTemp) {
            Helper::jsCloseWindow('参数错误!');
        } else {
            $arrAssign['arrContentData'] = $arrTemp;
        }
        if (isset($_POST['sub_btn'])) {
            $arrInsert = array(
                'content_id' => $arrAssign['content_id'],
                'user_id' => $this->arrCurManager['id'],
                'user_name' => $this->arrCurManager['nick_name'],
                'title' => isset($_POST['title']) ? $_POST['title'] : '',
                'txt' => isset($_POST['txt']) ? $_POST['txt'] : '',
            );
            $strFenCi = Helper::fenCi($arrInsert['title'] . ' ' . strip_tags($arrInsert['txt']));
            $arrInsert['search_index'] = Helper::genIndexStr($strFenCi);
            $mdlPage = new m_content_page();
            $idNew = $mdlPage->insert($arrInsert);
            if ($idNew) {
                $mdlPage->update(array('list_order' => $idNew), $idNew);
                Helper::jsCloseWindow('添加成功!', 'window.opener.location.reload();');
            } else {
                $arrAssign['arrData']['title'] = $arrInsert['title']; 
                $arrAssign['arrData']['txt'] = $arrInsert['txt'];
                $arrAssign['strMsg'] = '添加失败,请检查!';
            }
        }
        $this->_display('admin/content_page.php', $arrAssign);
    }
    
    public function add() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'content', 'add')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权添加内容!');
        }
        $objTreeList = $this->mdlCat->objTreeList;
        $arrAssign = array(
            'blnAdd' => true,
            'strTitle' => '添加内容',
            'objTreeList' => $objTreeList,
            'strOk' => '',
            'arrData' => array(
                'thumb' => '',
                'cat_id' => isset($_GET['cat_id']) ? intval($_GET['cat_id']) : 0
            )
        );
        if (isset($_POST['sub_btn'])) {
            $bln = true;
            $strMsg = '';
            $arrInsert = array('rec_status' => 0);
            if (!isset($_POST['cat_id']) || ($_POST['cat_id'] > 0 && !isset($objTreeList->arrIdAll['_' . $_POST['cat_id']]))) {
                $bln = false;
                $strMsg = '栏目参数错误!';
            }
            if ($bln) {
                if ($_POST['cat_id'] > 0) {
                    $arrTemp = $objTreeList->arrIdAll['_' . $_POST['cat_id']];
                    if ($arrTemp['rec_status'] == 0) {
                        $bln = false;
                        $strMsg = '栏目现不可用,请检查!';
                    } else {
                        $arrInsert['cat_id'] = $_POST['cat_id'];
                    }
                } else {
                    $arrInsert['cat_id'] = 0;
                }
            }
            if ($bln) {
                if (!isset($_POST['title']) || empty($_POST['title'])) {
                    $bln = false;
                    $strMsg = '请输入内容标题!';
                } else {
                    $arrInsert['title'] = $_POST['title'];
                }
            }
            isset($_POST['source_from']) && $arrInsert['source_from'] = $_POST['source_from'];
            isset($_POST['thumb']) && $arrInsert['thumb'] = $_POST['thumb'];
            isset($_POST['desc']) && $arrInsert['desc'] = $_POST['desc'];
            isset($_POST['tags']) && $arrInsert['tags'] = $_POST['tags'];
            isset($_POST['seo_t']) && $arrInsert['seo_t'] = $_POST['seo_t'];
            empty($arrInsert['seo_t']) && $arrInsert['seo_t'] = $arrInsert['title'];
            isset($_POST['seo_k']) && $arrInsert['seo_k'] = $_POST['seo_k'];
            empty($arrInsert['seo_k']) && $arrInsert['seo_k'] = $arrInsert['tags'];
            isset($_POST['seo_d']) && $arrInsert['seo_d'] = $_POST['seo_d'];
            if ($bln) {
                $arrInsert['user_id'] = $this->arrCurManager['id'];
                $arrInsert['user_name'] = $this->arrCurManager['nick_name'];
                $strFenCi = Helper::fenCi($arrInsert['title'] . ' ' . $arrInsert['desc'] . ' ' . $arrInsert['tags']);
                $arrInsert['search_index'] = Helper::genIndexStr($strFenCi);
                $idNew = $this->mdlContent->insert($arrInsert);
                if ($idNew) {
                    $this->mdlContent->update(array('list_order' => $idNew), $idNew);
                    Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=content&a=edit&id=' . $idNew, '内容添加成功!');
                } else {
                    $strMsg = '未能保存,请检查!';
                }
            }
            $arrAssign['strMsg'] = $strMsg;
            $arrAssign['arrData'] = $arrInsert;
        }
        $this->_display('admin/content_edit.php', $arrAssign);
    }
}